<?php
namespace app\admin\logic;
use app\admin\model\Gcategory;
use app\admin\model\GoodsPropValue;
use think\facade\Request;
use think\facade\View;

class GoodsLogic{
    public static function setLogic(){

    }
    //商品页
    //封商品where拼接条件
    public static function setWhere(){
        $param=Request::param();
        $where=[];

        if (isset($param['keywords']) && !empty($param['keywords'])){
            $where[]=['g.goods_name','like','%'.$param['keywords'].'%'];
//            $where[]=["<style color='red'>{$param['keywords']}</style>"];
//            if (isset($param['keywords'])){
//                $where[]=['g.goods_name' <style corlor="red"></style>]
//            }
        }
        if (isset($param['if_show']) && count($param['if_show'])==1){
            //$where[]=['if_show','in',$param['if_show']];
            $where[]=['g.if_show','=',$param['if_show'][0]];
        }
        if (isset($param['is_show']) && count($param['is_show'])==1){
            //$where[]=['if_show','in',$param['if_show']];
            $where[]=['g.is_show','=',$param['is_show'][0]];
        }
        if (isset($param['recommended']) && !empty($param['recommended']<>'')){
            $where[]=['g.recommended','=',$param['recommended']];
        }
        if (isset($param['maxtime']) && !empty($param['maxtime'])){
            $max=strtotime($param['maxtime']);
            $where[]=['g.add_time','<=',$max];
        }
        if (isset($param['mintime']) && !empty($param['mintime'])){
            $min=strtotime($param['mintime']);
            $where[]=['g.add_time','>=',$min];
        }
        if (isset($param['maxprice']) && !empty($param['maxprice'])){

            $where[]=['g.price','<=',$param['maxprice']];
        }
        if (isset($param['minprice']) && !empty($param['minprice'])){

            $where[]=['g.price','>=',$param['minprice']];
        }
        if (isset($param['pinpai']) && !empty($param['pinpai'])){
            $where[]=['g.brand','=',$param['pinpai']];
        }
        if (isset($param['cid']) && !empty($param['cid'])){
            $where[]=['g.cate_id','=',$param['cid']];
        }

//        echo "<pre>";
//        print_r($param);
//        echo "</pre>";

        return $where;
    }
    //封留值设置
    public static function setData(){
        $param=Request::param();
        $data=[
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'if_show'=>isset($param['if_show'])?$param['if_show']:[],
            'is_show'=>isset($param['is_show'])?$param['is_show']:[],
            'recommended'=>isset($param['recommended'])?$param['recommended']:'',
            // 'id'=>isset($param['id'])?$param['id']:'',
            'maxtime'=>isset($param['maxtime'])?$param['maxtime']:'',
            'mintime'=>isset($param['mintime'])?$param['mintime']:'',
            'maxprice'=>isset($param['maxprice'])?$param['maxprice']:'',
            'minprice'=>isset($param['minprice'])?$param['minprice']:'',
            'pinpai'=>isset($param['pinpai'])?$param['pinpai']:'',
            'cid'=>isset($param['cid'])?$param['cid']:'',

        ];

        return $data;
    }
    //排序
    public static function getOrder(){
        $id = Request::param("id")??0;
        $getorder="";
        if ($id==1){
            $getorder="add_time desc";
        }else if($id==2){
            $getorder="add_time ";
        }else if($id==3){
            $getorder="goods_id";
            $id=0;
        }
        View::assign("id",$id);
        return $getorder;

    }



    //属性页
    //属性留值设置
    public static function liuData(){
        $param=Request::param();
        $data=[
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'vid'=>isset($param['vid'])?$param['vid']:[],
        ];
        return $data;
    }
    //where条件
    public static function pinWhere(){
        $param=Request::param();
        $findId=[];
        $where=[];
        if (isset($param['keywords'])){
            $where[]=['pname','like',"%{$param['keywords']}%"];
        }
        if (isset($param['vid']) && !empty($param['vid'])){
            foreach ($param['vid'] as $v){
              $findId[]=['pvalues','find in set',$v];
            }
        }
        return [$where,$findId];

    }

    //品牌页
    //留值
    public static  function bData(){
        $param=Request::param();
        $data=[
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'recommended'=>isset($param['recommended'])?$param['recommended']:'',
            'if_show'=>isset($param['if_show'])?$param['if_show']:[],
            'brand_logo'=>isset($param['brand_logo'])?$param['brand_logo']:[],
        ];
        return $data;
    }
    //拼where条件
    public static function bWhere(){
        $param=Request::param();
        $where=[];
        $whereOr=[];

        if (isset($param['keywords'])){
            $where[]=['brand_name','like',"%".$param['keywords']."%"];
        }
        if(isset($param['recommended']) && !empty($param['recommended']<>'')){
            $where[]=['recommended','=',$param['recommended']];
        }
        if (isset($param['if_show']) && count($param['if_show'])==1){
            $where[]=['if_show','in',$param['if_show']];

        }
        if (isset($param["brand_logo"]) && count(['brand_logo'])==1){
            if($param['brand_logo'][0]=='0') {
                $where[] = ['brand_logo','=',''];
                $whereOr[] = ['brand_logo','=',null];
            }
            else if ($param['brand_logo'][0]=='1'){
                $where[]=['brand_logo','<>',''];

            }


        }


        return [$where,$whereOr];

    }

     //分类管理
    //处理数据  获取parent_id
    public static function  newData(){
        $param=Request::param();
        $data['parent_id']=isset($param['parent_id'])?$param['parent_id']:0;
        return $data;
    }
    //获取下一级分类  是否可以点击
    public static  function getNextCate($list){
        $parentId=Gcategory::getParentId();
        $parentId=array_unique($parentId);
        foreach ($list as &$vo){
            if (in_array($vo['cate_id'],$parentId))   $vo['next_cate']=1;


        }
        return $list;
    }

    //逗号
    public static function setDou($plist){
        foreach ($plist as &$vo){
            $pvalue=GoodsPropValue::getPropValue($vo['pvalues']);
            $vo['pvalues']=implode(',',$pvalue);

        }
        return $plist;
    }
}
